<?php

/**
 * @author Marc André Audet <root@manhim.net>
 * @copyright Copyright(c) 2009-2010 Marc André Audet
 * @version 2.0.5.23
 * @license http://www.gnu.org/licenses/gpl.txt The GNU General Public License version 3
 * 
 *   Manhim's Object Oriented framework
 *   Version consist of:
 *   [Major Version].[Number of years since 2010].[Month].[Date]
 *   The informations are based on the latest edit of this file, not the whole project.
 * 
 *   Copyright (C) 2009-2010 Marc André Audet
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
/*
 * Configuration part
 */

$cfg = array();
$cfg['default_module'] = 'GStatspp';
$cfg['default_args'] = array();
$cfg['output_buffering'] = true;
$cfg['gz_output'] = true;
$cfg['mb_output'] = true;
$cfg['timezone'] = 'America/Montreal';


/*
 * We set the error reporting and we define that we are in the
 * framework for Modules. We also define some constants.
 */

session_start();
error_reporting(E_ALL & ~E_NOTICE);
define('IN_MANHIM_FRAMEWORK', true);

define('FILES_EXTENSION', '.php');
define('FILES_EXTENSION_MODULES', '.module' . FILES_EXTENSION);
define('MODULES_CLASS_PREFIX', 'FModule_');


/*
 * We set the output buffers if required
 */

/**
 * This function check if the user agent supports gz_output output
 * buffer function by matching buggy browsers.
 * returns true if it matches a buggy browser, otherwise it returns
 * false.
 * @return boolean
 */
function isBuggyIe()
{
    $ua = $_SERVER['HTTP_USER_AGENT'];

    if (0 !== strpos($ua, 'Mozilla/4.0 (compatible; MSIE ') || false !== strpos($ua, 'Opera'))
    {
        return false;
    }

    $version = (float) substr($ua, 30);
    return ($version < 6 || ( $version == 6 && false === strpos($ua, 'SV1')));
}

header('Content-type: text/html; charset=UTF-8');
date_default_timezone_set($cfg['timezone']);

if ($cfg['output_buffering'] === true)
{
    if (!isBuggyIe() || $cfg['gz_output'] === true)
    {
        ob_start('ob_gzhandler');
    }
    else
    {
        ob_start();
    }

    if ($cfg['mb_output'] === true)
    {
        mb_http_output('UTF-8');
        ob_start('mb_output_handler');
    }
    else
    {
        ob_start();
    }
}


/*
 * Check if we have an argument input (CLI)
 * We set the module as the first argument (With lower priority versus the query string)
 *
 * TODO: Untested yet
 */

$module = null;
$args = null;
$args['ARGV'] = false;

if ($argv[1] && $argv[1] != '')
{
	$args['ARGV'] = true;
	$args['ARGV_VALUE'] = $argv;
	$module = $argv[1];
}


/*
 * Verify and parse the query string
 */

$pattern = '/([a-zA-Z0-9_-]+)(.*)/';
$matches = null;
$matches2 = null;

preg_match($pattern, $_SERVER['QUERY_STRING'], $matches);

if (isset($matches[2]))
{
    $pattern = '/([a-zA-Z0-9_-]+)[\\/|=]([a-zA-Z0-9_-]+)/';
    preg_match_all($pattern, $matches[2], $matches2);
}

if (isset($matches[1]))
{
    $module = $matches[1];
    if (isset($matches[2]) && isset($matches2[0]) && count($matches2[0]) > 0)
    {
        $args = array();
        for ($i = 0; $i < count($matches2[0]); $i++)
        {
            $args['' . $matches2[1][$i] . ''] = $matches2[2][$i];
        }
		$args['QUERY_STRING'] = (strlen($matches[2]) > 2 ? substr($matches[2], 1, strlen($matches[2])) : '');
    }
    else $args = null;
}
else if ($module === null || !$module || $module == '')
{
    $module = $cfg['default_module'];
    $args = $cfg['default_args'];
}


/*
 * Include every possible files dynamicaly
 */

$modules_folder = './Modules';
$modules_files = array();

$folders = array(
	'./Constants',
    './Functions',
    './Interfaces',
    './Abstracts',
    './Classes',
    $modules_folder
);

$have_main_module = false;
$folders_count = count($folders);
for ($i = 0; $i < $folders_count; $i++)
{
    $dir = opendir($folders[$i] . '/');
    while ($file = readdir($dir))
    {
        if ($file == '.' || $file == '..' || substr($file, 0, 1) == '.' || substr($file, strlen(FILES_EXTENSION) * (-1), strlen(FILES_EXTENSION)) != FILES_EXTENSION || is_dir($folders[$i] . '/' . $file))
        {
            continue;
        }

        require_once $folders[$i] . '/' . $file;

        if ($folders[$i] == $modules_folder)
        {
            $modules_files[] = $file;
            if (substr($file, 0, strlen(FILES_EXTENSION_MODULES) * (-1)) == $module)
            {
                $have_main_module = true;
            }
        }
    }
}

if ($have_main_module === false)
{
    exit();
}


/*
 * Add the names of every available modules to the modules list
 */

$modules_list = new Framework_Modules_List($args);

$modules_files_count = count($modules_files);
for ($i = 0; $i < $modules_files_count; $i++)
{
    if (substr($modules_files[$i], strlen(FILES_EXTENSION_MODULES) * (-1), strlen(FILES_EXTENSION_MODULES)) == FILES_EXTENSION_MODULES)
    {
        $name = substr($modules_files[$i], 0, strlen(FILES_EXTENSION_MODULES) * (-1));
        $modules_list->AddModule($name);
    }
}


/*
 * Verify if the target module is a Main module, then construct it
 * and run the Main() function.
 */

if ($modules_list->$module->GetIsMainModule())
{
    $modules_list->$module->Main();
}

?>